﻿using UnityEngine;
using System.Collections;

public class CameraBehaviour : MonoBehaviour {

	private bool ready;

	private Vector2 velocity;
	private Vector2 destinity;

	private float speed = 3;

	bool isFirst;
	bool playerFind;
	public void InsertTarget(Vector3 position)
	{
		ready = false;
		destinity = position;

		velocity.x = (position.x-transform.position.x);
		velocity.y = (position.y-transform.position.y);

		velocity.Normalize();

		ready = true;
		isFirst = true;
		playerFind = false;
	}

	void Update () 
	{


		if(ready)
		{
			if(Mathf.Abs(transform.localPosition.x) > 1)
			{
				velocity.x = 0;
				transform.Translate(transform.localPosition.x*-.05f*Time.deltaTime,0,0);
			}
			if(Mathf.Abs(transform.localPosition.y) > 1)
			{
				velocity.y = 0;
				transform.Translate(0,transform.localPosition.y*-.05f*Time.deltaTime,0);

			}

			transform.Translate(new Vector3(velocity.x*Time.deltaTime*speed,velocity.y*Time.deltaTime*speed,0),Space.Self);
			
			if (Mathf.Abs(destinity.x - transform.position.x) < .2)
			{
				velocity.x = 0;
			}
			if (Mathf.Abs(destinity.y - transform.position.y) < .2)
			{
				velocity.y = 0;
			}
			
			if (velocity.x == 0 && velocity.y == 0)
			{
				ready = false;
			}
		}
		else
		{
			transform.localPosition = new Vector3(0,0,-10);
		}
	}
}
